package repositories

import (
	"database/sql"
	"iris_product/common"
	"iris_product/datamodels"
)

// 定义接口
type IProduct interface {
	// 连接数据库
	Conn()(err error)
	Insert(product *datamodels.Product)(id int64,err error)
	Delete(id int64)(bool)
	Update(product *datamodels.Product)(err error)
	SelectById(id int64)(product *datamodels.Product,err error)
	SelectAll()(productList []*datamodels.Product,err error)
	SubProductNumOne(id int64) (err error)
}

// 定义resp struct
type ProductManager struct {
	table string
	mysqlConn *sql.DB
}

// 初始化mysql连接
func (p *ProductManager) Conn() (err error) {
	if p.mysqlConn == nil {
		// 连接mysql
		p.mysqlConn, err = common.NewMysqlConn()
		if err != nil {
			return
		}
	}
	return
}

func (p *ProductManager) Insert(product *datamodels.Product) (id int64, err error) {
	// 判断连接是否存在
	if err = p.Conn(); err != nil {
		return
	}
	if p.table == "" {
		p.table = "product"
	}
	sqlStr := "insert " + p.table + " set productName = ?,productNum = ?,productImage = ?,productUrl = ?"
	stmt,err := p.mysqlConn.Prepare(sqlStr)
	if err != nil {
		return
	}
	res,err := stmt.Exec(product.ProductName,product.ProductNum,product.ProductImage,product.ProductUrl)
	if err != nil {
		return
	}
	id,err = res.LastInsertId()
	if err != nil {
		return
	}
	return
}

// 商品删除
func (p *ProductManager) Delete(id int64) bool {
	var (
		err error
	)
	if err = p.Conn(); err != nil {
		return false
	}
	if p.table == "" {
		p.table = "product"
	}
	sqlStr := "delete from " + p.table + " where id = ?"
	stmt, err := p.mysqlConn.Prepare(sqlStr)
	if err != nil {
		return false
	}
	_, err = stmt.Exec(id)
	if err != nil {
		return false
	}
	return true

}

// 更新单条记录
func (p *ProductManager) Update(product *datamodels.Product) (err error) {
	if err = p.Conn(); err != nil {
		return err
	}
	if p.table == "" {
		p.table = "product"
	}
	sqlStr := "update " + p.table + " set  productName = ?,productNum = ?,productImage = ?,productUrl = ? where id = ?"
	stmt, err := p.mysqlConn.Prepare(sqlStr)
	defer stmt.Close()
	if err != nil {
		return err
	}
	_, err = stmt.Exec(product.ProductName, product.ProductNum, product.ProductImage, product.ProductUrl,product.ID)
	if err != nil {
		return err
	}
	return
}

// 数量自减1
func (p *ProductManager) SubProductNumOne(id int64) (err error) {
	if err = p.Conn(); err != nil {
		return err
	}
	if p.table == "" {
		p.table = "product"
	}
	sqlStr := "update " + p.table + " set  productNum = productNum - 1 where id = ?"
	stmt, err := p.mysqlConn.Prepare(sqlStr)
	defer stmt.Close()
	if err != nil {
		return err
	}
	_, err = stmt.Exec(id)
	if err != nil {
		return err
	}
	return
}

// 查询单条记录
func (p *ProductManager) SelectById(id int64) (product *datamodels.Product, err error) {

	product = &datamodels.Product{}

	if err = p.Conn(); err != nil {
		return
	}
	if p.table == "" {
		p.table = "product"
	}
	sqlStr := "select * from " + p.table + " where id = ?"
	stmt, err := p.mysqlConn.Prepare(sqlStr)
	if err != nil {
		return
	}
	rows,err := stmt.Query(id)
	defer rows.Close()
	if err != nil {
		return
	}
	record := common.GetResultRow(rows)
	if len(record) == 0 {
		// 没有查询到
		return
	}

	// 根据字段转为product
	common.DataToTagSqlByTag(record,product)

	return
}

// 商品列表
func (p *ProductManager) SelectAll() (productList []*datamodels.Product, err error) {
	productList = make([]*datamodels.Product,0)

	if err = p.Conn(); err != nil {
		return
	}
	if p.table == "" {
		p.table = "product"
	}
	sqlStr := "select * from " + p.table
	stmt, err := p.mysqlConn.Prepare(sqlStr)
	if err != nil {
		return
	}
	rows,err := stmt.Query()
	defer rows.Close()
	if err != nil {
		return
	}
	records := common.GetResultRows(rows)
	if len(records) == 0 {
		// 没有查询到
		return
	}

	for i := 0; i < len(records); i++ {
		product := &datamodels.Product{}
		// 根据字段转为product
		common.DataToTagSqlByTag(records[i],product)
		productList = append(productList,product)
	}

	return


}

// 构造函数
func NewProductManager(table string, conn *sql.DB) IProduct {
	return &ProductManager{
		table:     table,
		mysqlConn: conn,
	}
}